package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CalculateActivity extends AppCompatActivity {
    EditText et_first_num,et_second_num;
    CheckBox Cb_jia,Cb_jian,cb_chen,cb_chu;
    TextView tv_jia,tv_jian,tv_chen,tv_chu;
    int num1,num2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate);
        instan();
        et_first_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        Cb_jia.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    gainText();
                    int num= num1+num2;
                    tv_jia.setText(num1+"+"+num2+"="+num+"");
                }else{
                    tv_jia.setText("");
                }
            }
        });
        Cb_jian.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    gainText();
                    int num= num1-num2;
                    tv_jian.setText(num1+"-"+num2+"="+num+"");
                }else{
                    tv_jian.setText("");
                }
            }
        });
        cb_chen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    gainText();
                    int num= num1*num2;
                    tv_chen.setText(num1+"*"+num2+"="+num+"");
                }else{
                    tv_chen.setText("");
                }
            }
        });
        cb_chu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    gainText();
                    int num= num1/num2;
                    tv_chu.setText(num1+"/"+num2+"="+num+"");
                }else{
                    tv_chu.setText("");
                }
            }
        });
    }

    private void instan() {
        et_first_num=findViewById(R.id.et_first_num);
        et_second_num=findViewById(R.id.et_second_num);
        Cb_jia=findViewById(R.id.Cb_jia);
        Cb_jian=findViewById(R.id.Cb_jian);
        cb_chen=findViewById(R.id.cb_chen);
        cb_chu=findViewById(R.id.cb_chu);
        tv_jia=findViewById(R.id.jia);
        tv_jian=findViewById(R.id.jian);
        tv_chen=findViewById(R.id.chen);
        tv_chu=findViewById(R.id.chu);

    }
    public void gainText(){
        num1 = Integer.parseInt(et_first_num.getText().toString());
        num2 = Integer.parseInt(et_second_num.getText().toString());
    }
}
